package com.web.controller;

import com.github.pagehelper.PageInfo;
import com.web.dto.DoctorDTO;
import com.web.model.DoctorInfo;
import com.web.response.Result;
import com.web.service.DoctorService;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * @author yangguanzan
 * @date 9/24/21
 * @description
 */
@Controller
@RequestMapping("/hospital/doctor")
public class DoctorController {

    @Autowired
    DoctorService doctorService;

    @GetMapping(value = {"/",""})
    public String index(ModelMap modelMap,Integer pageNum,
        @ModelAttribute("name") String name,
        @ModelAttribute("department") String department){
        Map map = new HashMap(2);
        map.put("name",name);
        map.put("department",department);
        PageInfo<DoctorDTO> list=doctorService.findPage(pageNum,3,map);
        modelMap.addAttribute("doctors",list);
        return  "doctor/index";
    }

    @GetMapping(value = "/add")
    public String addPage(){
        return  "doctor/add";
    }

    @PostMapping(value = "/add")
    public String addPage(@ModelAttribute("doctor") DoctorInfo doctor){
        //进入服务层进行医生信息的插入
        Result result = doctorService.add(doctor);
        //根据结果判断成功则重定向回列表，失败则回到原来的jsp
        if(!result.isSuccess()){
            return  "doctor/add";
        }
        return  "redirect:/hospital/doctor";
    }


    @GetMapping(value = "/edit/{id}")
    public String updatePage(Model model,@PathVariable("id") Long id){
        // 调用医生服务，根据主键查询
        Result result = doctorService.findById(id);
        //根据结果判断成功则重定向回列表，失败则回到原来的jsp
        if(!result.isSuccess()){
            return  "redirect:/hospital/doctor";
        }
        //
        model.addAttribute("doctor",result.getData());
        return  "doctor/edit";
    }

    @PostMapping(value = "/edit")
    public String updatePage(@ModelAttribute("doctor") DoctorInfo doctor){
        //进入服务层进行医生信息的插入
        Result result = doctorService.update(doctor);
        //根据结果判断成功则重定向回列表，失败则回到原来的jsp
        if(!result.isSuccess()){
            return  "doctor/edit";
        }
        return  "redirect:/hospital/doctor";
    }


    @GetMapping(value = "/delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable("id") Long id){
        // 调用医生信息删除服务
        //Result result = doctorService.deleteById(id);

        return  Result.success();
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public Result delete(String ids){
        // 调用医生信息批量删除服务
        //Result result = doctorService.deleteByIds(ids);
        return  Result.success();
    }

}
